import { queryPageDataReq, updateItemReq } from "./services";
import { namespace } from "./configFile";
import { message } from "antd";

export default {
  namespace,
  state: {
    init: false,
    data: {},
  },
  effects: {
    *pageInit({ params, callback }, { call, put }) {
      const res = yield call(queryPageDataReq, params);
      if (res) {
        yield put({
          type: "save",
          payload: {
            ...res,
          },
        });
        callback && callback(res);
      }
    },
    *submitItem({ params, callback }, { call }) {
      let res = yield call(updateItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
